package 设计模式.观察者模式.观察者实现方式;

import java.math.BigDecimal;
import java.util.LinkedList;
import java.util.List;

public class Stock {

    //观察者集合
    private final List<Observer>observers;

    public Stock() {
        observers=new LinkedList<>();
    }
    private BigDecimal price;

    public void setPrice(BigDecimal price){
        this.price=price;
        //变更所有观察者
        this.notifyObservers(price);
    }
    public void attach(Observer observer){
        observers.add(observer);
    }
    public void detach(Observer observer){
        observers.remove(observer);
    }
    public void notifyObservers(BigDecimal price){
        if(observers.isEmpty())return;
        for (Observer observer : observers) {
            observer.operate(price);
        }
    }
}
